<template>
  <div class="index">
    <!--head-->
    <div class="role-head">
      <p class="role-head-title"><i class="icon iconfont icon-rizhi"></i>日志</p>
      <div class="role-head-operation">
        <span class="role-head-operation-items" v-if="logsRead" @click="selectShow"><i class="icon iconfont icon-chaxun"></i><span style="display:block;float:left">查询</span></span>
        <span class="role-head-operation-items"><a style="color:#fff;" href="javascript:history.go(-1)"><span style="display:block;float:left;font-size:16px;">返回</span><i class="icon iconfont icon-jinru"></i></a></span>
      </div>
    </div>
    <!--head end-->
    <!--select-->
    <div class="role-select on">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form label-position="left" label-width="100px" class="aaa">
            <el-form-item label="操作起始日期" class="rolelabel">
              <el-date-picker class="role-select-ipt" type="date" placeholder="选择日期" v-model="select.visitTimeStart" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-form label-position="left" label-width="100px">
            <el-form-item label="操作结束日期" class="rolelabel">
              <el-date-picker class="role-select-ipt" type="date" placeholder="选择日期" v-model="select.visitTimeEnd" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- <el-col :span="12">
          <el-form label-position="left" label-width="100px">
            <el-form-item label="字典标识" class="rolelabel">
              <el-input class="role-select-ipt" v-model="select.dicMark"></el-input>
            </el-form-item>
          </el-form>
        </el-col> -->
        <el-col :span="6" :offset="16">
            <el-button @click="selectHide" class="colse">关    闭</el-button>
            <el-button class="select-submit" @click="getLogList">查    询</el-button>
        </el-col>
      </el-row>      
    </div>
    <!--select end-->
    <!--table-->
    <div class="role-table">
      <table id="role-table">
          <tbody class="role-table-title">
            <td>操作用户</td>
            <td>操作内容</td>
            <td>访问时间</td>
            <td>访问IP</td>
            <td style="text-indent:18px;">响应时间(ms)</td>
          </tbody>
          <tr v-if="empty">
            <td class="empty" colspan="5">暂无数据</td>
          </tr>
          <tr v-else v-for="items in logList">
            <td>{{items.controllerName}}</td>
            <td>{{items.controllerMethod}}</td>
            <td>{{items.visitTime}}</td>
            <td>{{items.visitIp}}</td>
            <td>{{items.runTime}}</td>
          </tr>
      </table>
    </div>
    <!--table end-->
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="13"
      layout="prev, pager, next, jumper"
      :total="parseInt(total)"><!--因为这里:total期望的值是Number类型，但是vue返回的是String类型，所以要parseInt()转换类型-->
    </el-pagination>
    <!--分页 end-->
  </div>
</template>

<script>
import roleGrant from "./../components/roleGrant";
import roleAdd from "./../components/roleAdd";
import axios from "axios";
export default {
  name: "log",
  components: {
    roleGrant,
    roleAdd,
  },
  data() {
    return {
      currentPage: 1,
      total: '',
      show: false,
      Addshow: false,
      empty: false,
      logsRead: '',
      logList: [],
      time: "",
      select: {
        requestParameter: "", //请求参数
        visitUser: "", //操作用户
        visitTimeStart: "", //操作起始日期
        visitTimeEnd: "", //操作起始结束日期
        currentPage: "", //当前页
        pageSize: 13, //每页行数
        sortby: "", //排序列
        order: "" //排序方式
      }
    };
  },
  mounted: function() {
    this.$nextTick(() => {
      //防止在登陆页没有登陆时候直接手动改变url访问
      this.initPage();
      //调用全局方法 设置index高度
      this.indexHeight();
      //表格高度
      $(".role-table").height($(window).height() - 329 + "px");
      //
      this.getLogList();
      //调用全局方法根据权限值显示对应信息
      this.grantType();
    });
  },
  methods: {

    handleCurrentChange(val) {
      this.currentPage = val;
      axios
        .get(logsURL, {
          headers: { Authorization: appDB.get("userInfo").token },
          params: {
            currentPage:this.currentPage,
            pageSize: 13
          }
        })
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            if (d.record.rows.length > 0) {
              // 处理返回数据，将时间戳转换为时间
              let a = d.record.rows.map(o => {
                return {
                  controllerName: o.controllerName,
                  controllerMethod: o.controllerMethod,
                  visitTime: this.getMyDate(o.visitTime),//将时间戳转换为时间
                  visitIp: o.visitIp,
                  runTime: o.runTime,
                };
              });
              this.logList = a;
            } else {
              this.empty = true;
            }
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
      
      // console.log(`当前页: ${val}`);
    },
    //查条件hide
    selectShow() {
      $(".role-select").removeClass("on");
    },
    //查询条件hide
    selectHide() {
      $(".role-select").addClass("on");
      this.select.visitTimeStart = "";
      this.select.visitTimeEnd = "";
      this.getLogList();
    },
    //获取系统日志
    getLogList() {
      axios
        .get(logsURL, {
          headers: { Authorization: appDB.get("userInfo").token },
          params: this.select
        })
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            this.total = d.record.totalPage;
            if (d.record.rows.length > 0) {
              // 处理返回数据，将时间戳转换为时间
              let a = d.record.rows.map(o => {
                return {
                  controllerName: o.controllerName,
                  controllerMethod: o.controllerMethod,
                  visitTime: this.getMyDate(o.visitTime),//将时间戳转换为时间
                  visitIp: o.visitIp,
                  runTime: o.runTime,
                };
              });
              this.logList = a;
            } else {
              this.empty = true;
            }
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
    },
    //将时间戳转换为时间
    getMyDate(str) {
      var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime =
          oYear +
          "-" +
          this.getzf(oMonth) +
          "-" +
          this.getzf(oDay) +
          " " +
          this.getzf(oHour) +
          ":" +
          this.getzf(oMin) +
          ":" +
          this.getzf(oSen); //最后拼接时间
      return oTime;
    },
    //补0操作
    getzf(num) {
      if (parseInt(num) < 10) {
        num = "0" + num;
      }
      return num;
    }
  }
};
</script>

<style scoped>
@import "../assets/css/elementUI.css";
.el-pagination{
  margin-top: 15px;
}
.role-select-ipt {
  width: 75% !important;
}
.role-head {
  width: 100%;
  float: left;
  color: #fff;
  margin: 70px 0 20px 0;
}
.role-head-title {
  float: left;
  font-size: 16px;
  line-height: 30px;
}
.icon-rizhi {
  font-size: 20px;
  margin-right: 6px;
  float: left;
}
.role-head-operation {
  /* width: 168px; */
  float: right;
}
.role-head-operation-items {
  height: 18px;
  line-height: 18px;
  padding: 0 20px;
  display: inline-block;
  cursor: pointer;
  border-right: 1px solid #fff;
  float: left;
}
.role-head-operation-items:last-child {
  float: left;
  border-right: 0;
  font-size: 16px;
  padding-right: 0;
}
.icon-chaxun {
  display: inline-block;
  margin-right: 4px;
  float: left;
}
.icon-xinzeng {
  display: inline-block;
  margin-right: 4px;
  font-size: 18px;
  float: left;
}
.icon-jinru {
  color: #35c126;
  font-size: 26px;
  display: inline-block;
}
.role-table {
  width: 100%;
  float: left;
  background: #fff;
  overflow-y: auto;
}
#role-table {
  width: 100%;
  border: 0;
  background: #fff;
  float: left;
}
#role-table tr td {
  padding: 12px 15px;
  border: 1px solid #cbcbcb;
}
#role-table tr:hover {
  background: #f0f0f0;
}
.role-table-title {
  width: 100%;
  background: #0a456f;
  color: #fff;
}
.role-table-title td {
  padding: 12px 15px;
  border-right: 1px solid #012842;
  border-bottom: 1px solid #cbcbcb;
}
.role-table-operation {
  display: flex;
  justify-content: space-around;
}
.role-table-operation i {
  color: #5594d1;
  font-size: 18px;
  cursor: pointer;
}
.role-select {
  width: 100%;
  height: 146px;
  padding: 20px 4%;
  overflow: hidden;
  float: left;
  background: #14466a;
  margin-bottom: 20px;
  transition: all ease-in-out 0.5s;
}
.role-select.on {
  width: 100%;
  height: 0;
  padding: 0 4%;
  background: #14466a;
  margin-bottom: 0;
}
.colse {
  border-radius: 30px;
  padding: 8px 40px;
  background: none;
  border: 1px solid #367fb6;
  color: #fff;
}
.select-submit {
  border-radius: 30px;
  padding: 8px 40px;
  background: #39b423;
  border: 1px solid #39b423;
  color: #fff;
}
.layui-layer-title {
  color: #000;
  font-size: 16px;
}
</style>

